package com.wandian.util
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * 系统提示
	 * @author long
	 */
	public class ToolTip extends Sprite
	{
		private var m_arrDOTips:Array;
		private var m_tipTxt:TextField;
		private static var m_stage:Stage;
		private static var m_ntxtcolor:uint=16777215;
		private static var m_ntxtsize:int=12;
		private static var m_nbordercolor:uint=0;
		private static var m_nbgcolor:uint=0;
		private static var m_nmaxtxtwidth:Number=200;
		private static var m_nalpha:Number=1;
		private static var m_uniqueInstance:ToolTip;

		public function ToolTip()
		{
			this.m_arrDOTips=new Array();
			return;
		} 

		private function get DOTips():Array
		{
			return this.m_arrDOTips;
		} 

		private static function getInstance():ToolTip
		{
			if (m_uniqueInstance == null)
			{
				m_uniqueInstance=new ToolTip;
				m_uniqueInstance.visible=false;
				m_uniqueInstance.m_tipTxt=new TextField();
				m_uniqueInstance.m_tipTxt.autoSize=TextFieldAutoSize.LEFT;
				m_uniqueInstance.m_tipTxt.selectable=false;
				m_uniqueInstance.addChild(m_uniqueInstance.m_tipTxt);
				m_stage.addChild(m_uniqueInstance);
			}
			return m_uniqueInstance;
		} 

		public static function removeToolTip():void
		{
			var index:int=0;
			while (index < getInstance().DOTips.length)
			{

				getInstance().DOTips[index].DO.removeEventListener(MouseEvent.ROLL_OVER, showtip);
				getInstance().DOTips[index].DO.removeEventListener(MouseEvent.ROLL_OUT, hidetip);
				getInstance().DOTips[index].DO.removeEventListener(MouseEvent.MOUSE_MOVE, movetip);
				getInstance().DOTips[index]=null;
				m_stage.removeChild(getInstance());
				m_uniqueInstance=null;
				index=index + 1;
			}
			return;
		} 

		public static function hideToolTip():void
		{
			try
			{
				m_stage.removeChild(getInstance());
			}
			catch (e:Error)
			{
			}
			return;
		} 

		public static function showToolTip():void
		{
			try
			{
				m_stage.addChild(getInstance());
			}
			catch (e:Error)
			{
			}
			return;
		} 

		public static function BindDO(DO:DisplayObject, info:String):void
		{
			var tipData:Object=null;
			if (TestDOBinding(DO) == -1)
			{
				tipData={DO: DO, info: info};
				getInstance().DOTips.push(tipData);
				DO.addEventListener(MouseEvent.ROLL_OVER, showtip);
				DO.addEventListener(MouseEvent.ROLL_OUT, hidetip);
				DO.addEventListener(MouseEvent.MOUSE_MOVE, movetip);
			}
			return;
		} 

		public static function LooseDO(param1:DisplayObject):void
		{
			var _loc_2:int=0;
			if (TestDOBinding(param1) != -1)
			{
				_loc_2=TestDOBinding(param1);
				while (_loc_2 < (getInstance().DOTips.length - 1))
				{

					getInstance().DOTips[_loc_2]=getInstance().DOTips[(_loc_2 + 1)];
					_loc_2=_loc_2 + 1;
				}
				getInstance().DOTips.pop();
				param1.removeEventListener(MouseEvent.ROLL_OVER, showtip);
				param1.removeEventListener(MouseEvent.ROLL_OUT, hidetip);
				param1.removeEventListener(MouseEvent.MOUSE_MOVE, movetip);
			}
			return;
		} 

		public static function setDOInfo(param1:DisplayObject, param2:String):void
		{
			if (TestDOBinding(param1) == -1)
			{
				BindDO(param1, param2);
			}
			else
			{
				getInstance().DOTips[TestDOBinding(param1)].info=param2;
			}
			return;
		} 

		public static function TestDOBinding(param1:DisplayObject):int
		{
			var _loc_2:Boolean=false;
			var _loc_3:int=0;
			while (_loc_3 < getInstance().DOTips.length)
			{

				if (getInstance().DOTips[_loc_3].DO == param1)
				{
					_loc_2=true;
					break;
				}
				_loc_3=_loc_3 + 1;
			}
			return _loc_2 ? (_loc_3) : (-1);
		} 

		private static function showtip(event:MouseEvent):void
		{
			var _loc_2:*=event.target as DisplayObject;
			getInstance().m_tipTxt.wordWrap=false;
			getInstance().m_tipTxt.textColor=m_ntxtcolor;
			getInstance().m_tipTxt.htmlText=getInstance().DOTips[TestDOBinding(event.target as DisplayObject)].info;
			if (_loc_2["disX"] == null)
			{
				getInstance().x=event.stageX - getInstance().m_tipTxt.width / 2;
				getInstance().y=event.stageY + 20;
			}
			else if (_loc_2["disX"] == 0)
			{
				getInstance().x=event.stageX - getInstance().m_tipTxt.width / 2;
				getInstance().y=_loc_2.y + _loc_2["disY"];
			}
			else
			{
				getInstance().x=_loc_2.x + _loc_2["disX"];
				getInstance().y=_loc_2.y + _loc_2["disY"];
			}
			if (getInstance().x + getInstance().width > m_stage.stageWidth)
			{
				getInstance().x=m_stage.stageWidth - getInstance().width;
			}
			if (getInstance().x < 0)
			{
				getInstance().x=0;
			}
			updatetip();
			getInstance().visible=true;
			return;
		} 

		private static function hidetip(event:MouseEvent):void
		{
			getInstance().visible=false;
			return;
		} 

		private static function movetip(event:MouseEvent):void
		{
			var mc:DisplayObject=event.target as DisplayObject;
			if (mc["disX"] == null)
			{
				getInstance().x=event.stageX - getInstance().m_tipTxt.width / 2;
				getInstance().y=event.stageY + 20;
			}
			else if (mc["disX"] == 0)
			{
				getInstance().x=event.stageX - getInstance().m_tipTxt.width / 2;
				getInstance().y=mc.y + mc["disY"];
			}
			else
			{
				getInstance().x=mc.x + mc["disX"];
				getInstance().y=mc.y + mc["disY"];
			}
			if (getInstance().x + getInstance().width > m_stage.stageWidth)
			{
				getInstance().x=m_stage.stageWidth - getInstance().width;
			}
			if (getInstance().x < 0)
			{
				getInstance().x=0;
			}
			return;
		} 

		private static function updatetip():void
		{
			if (getInstance().m_tipTxt.width > m_nmaxtxtwidth)
			{
				getInstance().m_tipTxt.wordWrap=true;
				getInstance().m_tipTxt.width=m_nmaxtxtwidth;
			}
			var _loc_1:*=new TextFormat();
			_loc_1.size=m_ntxtsize;
			getInstance().m_tipTxt.setTextFormat(_loc_1);
			var _loc_2:*=getInstance().graphics;
			_loc_2.clear();
			_loc_2.lineStyle(0, m_nbordercolor, m_nalpha);
			_loc_2.beginFill(m_nbgcolor, m_nalpha);
			_loc_2.drawRect(0, 0, getInstance().m_tipTxt.width, getInstance().m_tipTxt.height);
			_loc_2.endFill();
			return;
		} 

		public static function set stage(param1:Stage):void
		{
			m_stage=param1;
			return;
		} 

		public static function setTipProperty(ntxtcolor:uint=0, ntxtsize:int=12, nmaxtxtwidth:int=200, nbordercolor:uint=0, nbgcolor:uint=16777164, nalpha:Number=1):void
		{
			m_ntxtcolor=ntxtcolor;
			m_ntxtsize=ntxtsize;
			m_nmaxtxtwidth=nmaxtxtwidth;
			m_nbordercolor=nbordercolor;
			m_nbgcolor=nbgcolor;
			m_nalpha=nalpha;
			return;
		} 

	}
}
